package icu.ikuns;

import java.sql.*;

/**
 * @Author： Kimariyb
 * @Date：2022/05/14/19:09
 * @Description：
 */
public class QuestionBank {
    private int currentPosition = 0;
    private static Question[] questions = {
            new Question("010001", 0, "黄山在中国的安徽省。", "correct"),
            new Question("010002", 0, "北京是中国的首都，简称京。", "correct"),
            new Question("010003", 0, "中国钢产量最多的省是湖南。", "wrong"),
            new Question("010004", 0, "新疆是中国面积最大的省级行政区。", "correct")
    };
    public void setCurrentPosition(int p) {
        currentPosition = p;
    }
    public int getCurrentPosition() {
        return currentPosition;
    }
    public Question getCurrentQuestion() {
        return questions[currentPosition];
    }

    public int addQuestion(Question q) {
        int retcode = 0;
        if (q == null) {
            return 0;
        }
        Connection conn = null;
        PreparedStatement pstmt = null;
        Statement sta = null;
        ResultSet rs = null;
        try {
            conn = DBConnection.getConnection();
            sta = conn.createStatement();
            rs = sta.executeQuery("select * from questions where questionId=\"" + q.getQuestionId() + "\"");
            if (rs.next()) {
                retcode = 2;
            } else {
                String sql = "insert into questions (questionId, questionClass, questionContent, questionAnswer) " +
                        "values (?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, q.getQuestionId());
                pstmt.setInt(2, q.getQuestionClass());
                pstmt.setString(3, q.getQuestionContent());
                pstmt.setString(4, q.getQuestionAnswer());
                retcode = pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBConnection.free(conn, sta, rs);
        }
        return retcode;
    }
}
